
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>1. 命令行与环境 &#8212; Python 3.7.3 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.3 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="2. 在Unix平台中使用Python" href="unix.html" />
    <link rel="prev" title="安装和使用 Python" href="index.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/using/cmdline.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/switchers.js"></script>
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="unix.html" title="2. 在Unix平台中使用Python"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="index.html" title="安装和使用 Python"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh_CN</span>
          <span class="version_switcher_placeholder">3.7.3</span>
          <a href="../index.html">文档</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">安装和使用 Python</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="command-line-and-environment">
<span id="using-on-general"></span><h1>1. 命令行与环境<a class="headerlink" href="#command-line-and-environment" title="永久链接至标题">¶</a></h1>
<p>CPython 解析器会扫描命令行与环境用于获取各种设置信息。</p>
<div class="impl-detail compound">
<p><strong>CPython implementation detail:</strong> 其他实现的命令行方案可能有所不同。 更多相关资源请参阅 <a class="reference internal" href="../reference/introduction.html#implementations"><span class="std std-ref">其他实现</span></a>。</p>
</div>
<div class="section" id="command-line">
<span id="using-on-cmdline"></span><h2>1.1. 命令行<a class="headerlink" href="#command-line" title="永久链接至标题">¶</a></h2>
<p>对 Python 发起调用时，你可以指定以下的任意选项:</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span>python <span class="o">[</span>-bBdEhiIOqsSuvVWx?<span class="o">]</span> <span class="o">[</span>-c <span class="nb">command</span> <span class="p">|</span> -m module-name <span class="p">|</span> script <span class="p">|</span> - <span class="o">]</span> <span class="o">[</span>args<span class="o">]</span>
</pre></div>
</div>
<p>当然最常见的用例就是简单地启动执行一个脚本:</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span>python myscript.py
</pre></div>
</div>
<div class="section" id="interface-options">
<span id="using-on-interface-options"></span><h3>1.1.1. 接口选项<a class="headerlink" href="#interface-options" title="永久链接至标题">¶</a></h3>
<p>解释器接口类似于 UNIX shell，但提供了一些额外的发起调用方法:</p>
<ul class="simple">
<li>当调用时附带连接到某个 tty 设备的标准输入时，它会提示输入命令并执行它们，直到读入一个 EOF（文件结束字符，其产生方式是在 UNIX 中按 <kbd class="kbd docutils literal notranslate">Ctrl-D</kbd> 或在 Windows 中按 <kbd class="kbd docutils literal notranslate">Ctrl-Z, Enter</kbd>。）</li>
<li>当调用时附带一个文件名参数或以一个文件作为标准输入时，它会从该文件读取并执行脚本程序。</li>
<li>当调用时附带一个目录名参数时，它会从该目录读取并执行具有适当名称的脚本程序。</li>
<li>当调用时附带 <code class="docutils literal notranslate"><span class="pre">-c</span> <span class="pre">command</span></code> 时，它会执行 <em>command</em> 所给出的 Python 语句。 在这里 <em>command</em> 可以包含以换行符分隔的多条语句。 请注意前导空格在 Python 语句中是有重要作用的！</li>
<li>当调用时附带 <code class="docutils literal notranslate"><span class="pre">-m</span> <span class="pre">module-name</span></code> 时，会在 Python 模块路径中查找指定的模块，并将其作为脚本程序执行。</li>
</ul>
<p>在非交互模式下，会对全部输入先解析再执行。</p>
<p>一个接口选项会终结解释器所读入的选项列表，后续的所有参数将被放入 <a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> -- 请注意其中首个元素即第零项 (<code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code>) 会是一个表示程序源的字符串。</p>
<dl class="cmdoption">
<dt id="cmdoption-c">
<code class="descname">-c</code><code class="descclassname"> &lt;command&gt;</code><a class="headerlink" href="#cmdoption-c" title="永久链接至目标">¶</a></dt>
<dd><p>执行 <em>command</em> 中的 Python 代码。 <em>command</em> 可以为一条或以换行符分隔的多条语句，其中前导空格像在普通模块代码中一样具有作用。</p>
<p>如果给出此选项，<a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> 的首个元素将为 <code class="docutils literal notranslate"><span class="pre">&quot;-c&quot;</span></code> 并且当前目录将被加入 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 的开头（以允许该目录中的模块作为最高层级模块被导入）。</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-m">
<code class="descname">-m</code><code class="descclassname"> &lt;module-name&gt;</code><a class="headerlink" href="#cmdoption-m" title="永久链接至目标">¶</a></dt>
<dd><p>在 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 中搜索指定名称的模块并将其内容作为 <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> 模块来执行。</p>
<p>由于该参数为 <em>module</em> 名称，你不应给出文件扩展名 (<code class="docutils literal notranslate"><span class="pre">.py</span></code>)。 模块名称应为绝对有效的 Python 模块名称，但具体实现可能并不总是强制要求这一点（例如它可能允许你使用包含连字符的名称）。</p>
<p>包名称（包括命名空间包）也允许使用。 当所提供的是包名称而非普通模块名称时，解释器将把 <code class="docutils literal notranslate"><span class="pre">&lt;pkg&gt;.__main__</span></code> 作为主模块来执行。 此行为特意被设计为与作为脚本参数传递给解释器的目录和 zip 文件的处理方式类似。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">此选项不适用于内置模块和以 C 编写的扩展模块，因为它们并没有对应的 Python 模块文件。 但是它仍然适用于预编译的模块，即使没有可用的初始源文件。</p>
</div>
<p>如果给出此选项，<a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> 的首个元素将为模块文件的完整路径 (在定位模块文件期间，首个元素将设为 <code class="docutils literal notranslate"><span class="pre">&quot;-m&quot;</span></code>)。 与 <a class="reference internal" href="#cmdoption-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c</span></code></a> 选项一样，当前目录将被加入 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 的开头。</p>
<p>许多标准库模块都包含作为脚本执行时会被发起调用的代码。 其中的一个例子是 <a class="reference internal" href="../library/timeit.html#module-timeit" title="timeit: Measure the execution time of small code snippets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">timeit</span></code></a> 模块:</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span>python -mtimeit -s <span class="s1">&#39;setup here&#39;</span> <span class="s1">&#39;benchmarked code here&#39;</span>
python -mtimeit -h <span class="c1"># for details</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="first admonition-title">参见</p>
<dl class="docutils">
<dt><a class="reference internal" href="../library/runpy.html#runpy.run_module" title="runpy.run_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">runpy.run_module()</span></code></a></dt>
<dd>Python 代码可以直接使用的等效功能</dd>
</dl>
<p class="last"><span class="target" id="index-69"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0338"><strong>PEP 338</strong></a> -- 将模块作为脚本执行</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">在 3.1 版更改: </span>提供包名称来运行 <code class="docutils literal notranslate"><span class="pre">__main__</span></code> 子模块。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">在 3.4 版更改: </span>同样支持命名空间包</p>
</div>
</dd></dl>

<dl class="describe">
<dt>
<code class="descname">-</code></dt>
<dd><p>从标准输入 (<a class="reference internal" href="../library/sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a>) 读取命令。 如果标准输入为一个终端，则使用 <a class="reference internal" href="#cmdoption-i"><code class="xref std std-option docutils literal notranslate"><span class="pre">-i</span></code></a>。</p>
<p>如果给出此选项，<a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> 的首个元素将为 <code class="docutils literal notranslate"><span class="pre">&quot;-&quot;</span></code> 并且当前目录将被加入 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 的开头。</p>
</dd></dl>

<dl class="describe">
<dt>
<code class="descname">&lt;script&gt;</code></dt>
<dd><p>执行 <em>script</em> 中的 Python 代码，该参数应为一个（绝对或相对）文件系统路径，指向某个 Python 文件、包含 <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> 文件的目录，或包含 <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> 文件的 zip 文件。</p>
<p>如果给出此选项，<a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> 的首个元素将为在命令行中指定的脚本名称。</p>
<p>如果脚本名称直接指向一个 Python 文件，则包含该文件的目录将被加入 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 的开头，并且该文件会被作为 <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> 模块来执行。</p>
<p>如果脚本名称指向一个目录或 zip 文件，则脚本名称将被加入 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 的开头，并且该位置中的 <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> 文件会被作为 <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> 模块来执行。</p>
<div class="admonition seealso">
<p class="first admonition-title">参见</p>
<dl class="last docutils">
<dt><a class="reference internal" href="../library/runpy.html#runpy.run_path" title="runpy.run_path"><code class="xref py py-func docutils literal notranslate"><span class="pre">runpy.run_path()</span></code></a></dt>
<dd>Python 代码可以直接使用的等效功能</dd>
</dl>
</div>
</dd></dl>

<p>如果没有给出接口选项，则使用 <a class="reference internal" href="#cmdoption-i"><code class="xref std std-option docutils literal notranslate"><span class="pre">-i</span></code></a>，<code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> 将为空字符串 (<code class="docutils literal notranslate"><span class="pre">&quot;&quot;</span></code>)，并且当前目录会被加入 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 的开头。 此外，tab 补全和历史编辑会自动启用，如果你的系统平台支持此功能的话 (参见 <a class="reference internal" href="../library/site.html#rlcompleter-config"><span class="std std-ref">Readline configuration</span></a>)。</p>
<div class="admonition seealso">
<p class="first admonition-title">参见</p>
<p class="last"><a class="reference internal" href="../tutorial/interpreter.html#tut-invoking"><span class="std std-ref">调用解释器</span></a></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">在 3.4 版更改: </span>自动启用 tab 补全和历史编辑。</p>
</div>
</div>
<div class="section" id="generic-options">
<h3>1.1.2. 通用选项<a class="headerlink" href="#generic-options" title="永久链接至标题">¶</a></h3>
<dl class="cmdoption">
<dt id="cmdoption">
<code class="descname">-?</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption" title="永久链接至目标">¶</a></dt>
<dt id="cmdoption-h">
<code class="descname">-h</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-h" title="永久链接至目标">¶</a></dt>
<dt id="cmdoption-help">
<code class="descname">--help</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-help" title="永久链接至目标">¶</a></dt>
<dd><p>打印全部命令行选项的简短描述。</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-v">
<code class="descname">-V</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-v" title="永久链接至目标">¶</a></dt>
<dt id="cmdoption-version">
<code class="descname">--version</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-version" title="永久链接至目标">¶</a></dt>
<dd><p>打印 Python 版本号并退出。 示例输出信息如下:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Python 3.7.0b2+
</pre></div>
</div>
<p>如果重复给出，则打印有关构建的更多信息，例如:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Python 3.7.0b2+ (3.7:0c076caaa8, Sep 22 2018, 12:04:24)
[GCC 6.2.0 20161005]
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">3.6 新版功能: </span><code class="docutils literal notranslate"><span class="pre">-VV</span></code> 选项。</p>
</div>
</dd></dl>

</div>
<div class="section" id="miscellaneous-options">
<span id="using-on-misc-options"></span><h3>1.1.3. 其他选项<a class="headerlink" href="#miscellaneous-options" title="永久链接至标题">¶</a></h3>
<dl class="cmdoption">
<dt id="cmdoption-b">
<code class="descname">-b</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-b" title="永久链接至目标">¶</a></dt>
<dd><p>在将 <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 或 <a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> 与 <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> 或是将 <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 与 <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> 比较时发出警告。 如果重复给出该选项 (<code class="xref std std-option docutils literal notranslate"><span class="pre">-bb</span></code>) 则会引发错误。</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.5 版更改: </span>影响 <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 与 <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> 的比较。</p>
</div>
</dd></dl>

<dl class="cmdoption">
<dt id="id1">
<code class="descname">-B</code><code class="descclassname"></code><a class="headerlink" href="#id1" title="永久链接至目标">¶</a></dt>
<dd><p>如果给出此选项，Python 将不会试图在导入源模块时写入 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件。 另请参阅 <span class="target" id="index-70"></span><a class="reference internal" href="#envvar-PYTHONDONTWRITEBYTECODE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONDONTWRITEBYTECODE</span></code></a>。</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-check-hash-based-pycs">
<code class="descname">--check-hash-based-pycs</code><code class="descclassname"> default|always|never</code><a class="headerlink" href="#cmdoption-check-hash-based-pycs" title="永久链接至目标">¶</a></dt>
<dd><p>控制基于哈希值的 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件的验证行为。 参见 <a class="reference internal" href="../reference/import.html#pyc-invalidation"><span class="std std-ref">已缓存字节码的失效</span></a>。 当设为 <code class="docutils literal notranslate"><span class="pre">default</span></code> 时，已选定和未选定的基于哈希值的字节码缓存文件将根据其默认语义进行验证。 当设为 <code class="docutils literal notranslate"><span class="pre">always</span></code> 时，所有基于哈希值的 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件，不论是已选定还是未选定的都将根据其对应的源文件进行验证。 当设为 <code class="docutils literal notranslate"><span class="pre">never</span></code> 时，基于哈希值的 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件将不会根据其对应的源文件进行验证。</p>
<p>基于时间戳的 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件的语义不会受此选项影响。</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-d">
<code class="descname">-d</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-d" title="永久链接至目标">¶</a></dt>
<dd><p>开启解析器调试输出（限专家使用，依赖于编译选项）。 另请参阅 <span class="target" id="index-71"></span><a class="reference internal" href="#envvar-PYTHONDEBUG"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONDEBUG</span></code></a>。</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-e">
<code class="descname">-E</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-e" title="永久链接至目标">¶</a></dt>
<dd><p>忽略所有 <span class="target" id="index-72"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHON*</span></code> 环境变量，例如可能已设置的 <span class="target" id="index-73"></span><a class="reference internal" href="#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> 和 <span class="target" id="index-74"></span><a class="reference internal" href="#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a>。</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-i">
<code class="descname">-i</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-i" title="永久链接至目标">¶</a></dt>
<dd><p>当有脚本被作为首个参数传入或使用了 <a class="reference internal" href="#cmdoption-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c</span></code></a> 选项时，在执行脚本或命令之后进入交互模式，即使是在 <a class="reference internal" href="../library/sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a> 并不是一个终端的时候。 <span class="target" id="index-75"></span><a class="reference internal" href="#envvar-PYTHONSTARTUP"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONSTARTUP</span></code></a> 文件不会被读取。</p>
<p>这一选项的用处是在脚本引发异常时检查全局变量或者栈跟踪。 另请参阅 <span class="target" id="index-76"></span><a class="reference internal" href="#envvar-PYTHONINSPECT"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONINSPECT</span></code></a>。</p>
</dd></dl>

<dl class="cmdoption">
<dt id="id2">
<code class="descname">-I</code><code class="descclassname"></code><a class="headerlink" href="#id2" title="永久链接至目标">¶</a></dt>
<dd><p>在隔离模式下运行 Python。 这将同时应用 -E 和 -s。 在隔离模式下 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 既不包含脚本所在目录也不包含用户的 site-packages 目录。 所有 <span class="target" id="index-77"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHON*</span></code> 环境变量也会被忽略。 还可以施加更进一步的限制以防止用户注入恶意代码。</p>
<div class="versionadded">
<p><span class="versionmodified">3.4 新版功能.</span></p>
</div>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-o">
<code class="descname">-O</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-o" title="永久链接至目标">¶</a></dt>
<dd><p>移除 assert 语句以及任何以 <a class="reference internal" href="../library/constants.html#__debug__" title="__debug__"><code class="xref py py-const docutils literal notranslate"><span class="pre">__debug__</span></code></a> 的值作为条件的代码。 通过在 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 扩展名之前添加 <code class="docutils literal notranslate"><span class="pre">.opt-1</span></code> 来扩充已编译文件 (<a class="reference internal" href="../glossary.html#term-bytecode"><span class="xref std std-term">bytecode</span></a>) 的文件名  (参见 <span class="target" id="index-78"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a>)。 另请参阅 <span class="target" id="index-79"></span><a class="reference internal" href="#envvar-PYTHONOPTIMIZE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONOPTIMIZE</span></code></a>。</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.5 版更改: </span>依据 <span class="target" id="index-80"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a> 修改 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件名。</p>
</div>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-oo">
<code class="descname">-OO</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-oo" title="永久链接至目标">¶</a></dt>
<dd><p>在启用 <a class="reference internal" href="#cmdoption-o"><code class="xref std std-option docutils literal notranslate"><span class="pre">-O</span></code></a> 的同时丢弃文档字符串。 通过在 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 扩展名之前添加 <code class="docutils literal notranslate"><span class="pre">.opt-2</span></code> 来扩展已编译文件 (<a class="reference internal" href="../glossary.html#term-bytecode"><span class="xref std std-term">bytecode</span></a>) 的文件名 (参见 <span class="target" id="index-81"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a>)。</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.5 版更改: </span>依据 <span class="target" id="index-82"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a> 修改 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件名。</p>
</div>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-q">
<code class="descname">-q</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-q" title="永久链接至目标">¶</a></dt>
<dd><p>即使在交互模式下也不显示版权和版本信息。</p>
<div class="versionadded">
<p><span class="versionmodified">3.2 新版功能.</span></p>
</div>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-r">
<code class="descname">-R</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-r" title="永久链接至目标">¶</a></dt>
<dd><p>开启哈希随机化。 此选项权 <span class="target" id="index-83"></span><a class="reference internal" href="#envvar-PYTHONHASHSEED"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHASHSEED</span></code></a> 环境变量设置为 <code class="docutils literal notranslate"><span class="pre">0</span></code> 时起作用，因为哈希随机化是默认启用的。</p>
<p>在先前的 Python 版本中，此选项会开启哈希随机化，这样 str, bytes 和 datetime 的 <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code></a> 值将会使用一个不可预测的随机值来“加盐”。 虽然它们在单个 Python 进程中会保持不变，但在重复发起的 Python 调用之间则是不可预测的。</p>
<p>哈希随机化旨在针对由精心选择的输入引起的拒绝服务攻击提供防护，这种输入利用了构造 dict 在最坏情况下的性能即 O(n^2) 复杂度。 详情请参阅 <a class="reference external" href="http://www.ocert.org/advisories/ocert-2011-003.html">http://www.ocert.org/advisories/ocert-2011-003.html</a>。</p>
<p><span class="target" id="index-84"></span><a class="reference internal" href="#envvar-PYTHONHASHSEED"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHASHSEED</span></code></a> 允许你为哈希种子密码设置一个固定值。</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.7 版更改: </span>此选项不会再被忽略。</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">3.2.3 新版功能.</span></p>
</div>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-s">
<code class="descname">-s</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-s" title="永久链接至目标">¶</a></dt>
<dd><p>不要将 <a class="reference internal" href="../library/site.html#site.USER_SITE" title="site.USER_SITE"><code class="xref py py-data docutils literal notranslate"><span class="pre">用户</span> <span class="pre">site-packages</span> <span class="pre">目录</span></code></a> 添加到 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>。</p>
<div class="admonition seealso">
<p class="first admonition-title">参见</p>
<p class="last"><span class="target" id="index-85"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0370"><strong>PEP 370</strong></a> -- 分用户的 site-packages 目录</p>
</div>
</dd></dl>

<dl class="cmdoption">
<dt id="id3">
<code class="descname">-S</code><code class="descclassname"></code><a class="headerlink" href="#id3" title="永久链接至目标">¶</a></dt>
<dd><p>禁用 <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> 的导入及其所附带的基于站点对 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 的操作。 如果 <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> 会在稍后被显式地导入也会禁用这些操作 (如果你希望触发它们则应调用 <a class="reference internal" href="../library/site.html#site.main" title="site.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">site.main()</span></code></a>)。</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-u">
<code class="descname">-u</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-u" title="永久链接至目标">¶</a></dt>
<dd><p>强制 stdout 和 stderr 流不使用缓冲。 此选项对 stdin 流无影响。</p>
<p>另请参阅 <span class="target" id="index-86"></span><a class="reference internal" href="#envvar-PYTHONUNBUFFERED"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONUNBUFFERED</span></code></a>。</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.7 版更改: </span>stdout 和 stderr 流在文本层现在不使用缓冲。</p>
</div>
</dd></dl>

<dl class="cmdoption">
<dt id="id4">
<code class="descname">-v</code><code class="descclassname"></code><a class="headerlink" href="#id4" title="永久链接至目标">¶</a></dt>
<dd><p>每当一个模块被初始化时打印一条信息，显示其加载位置（文件名或内置模块）。 当重复给出时 (<code class="xref std std-option docutils literal notranslate"><span class="pre">-vv</span></code>)，为搜索模块时所检查的每个文件都打印一条消息。 此外还提供退出时有关模块清理的信息A。 另请参阅 <span class="target" id="index-87"></span><a class="reference internal" href="#envvar-PYTHONVERBOSE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONVERBOSE</span></code></a>。</p>
</dd></dl>

<span class="target" id="using-on-warnings"></span><dl class="cmdoption">
<dt id="cmdoption-w">
<code class="descname">-W</code><code class="descclassname"> arg</code><a class="headerlink" href="#cmdoption-w" title="永久链接至目标">¶</a></dt>
<dd><p>警告控制。 Python 的警告机制在默认情况下会向 <a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> 打印警告消息。 典型的警告消息具有如下形式：</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>file:line: category: message
</pre></div>
</div>
<p>默认情况下，每个警告都对于其发生所在的每个源行都会打印一次。 此选项可控制警告打印的频繁程度。</p>
<p>可以给出多个 <a class="reference internal" href="#cmdoption-w"><code class="xref std std-option docutils literal notranslate"><span class="pre">-W</span></code></a> 选项；当某个警告能与多个选项匹配时，将执行最后一个匹配选项的操作。 无效的 <a class="reference internal" href="#cmdoption-w"><code class="xref std std-option docutils literal notranslate"><span class="pre">-W</span></code></a> 选项将被忽略（但是，在发出第一个警告时会打印有关无效选项的警告消息）。</p>
<p>警告也可以使用 <span class="target" id="index-88"></span><a class="reference internal" href="#envvar-PYTHONWARNINGS"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONWARNINGS</span></code></a> 环境变量以及使用 <a class="reference internal" href="../library/warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code></a> 模块在 Python 程序内部进行控制。</p>
<p>最简单的设置是将某个特定操作无条件地应用于进程所发出所有警告 (即使是在默认情况下会忽略的那些警告):</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span>-Wdefault  <span class="c1"># Warn once per call location</span>
-Werror    <span class="c1"># Convert to exceptions</span>
-Walways   <span class="c1"># Warn every time</span>
-Wmodule   <span class="c1"># Warn once per calling module</span>
-Wonce     <span class="c1"># Warn once per Python process</span>
-Wignore   <span class="c1"># Never warn</span>
</pre></div>
</div>
<p>操作名称可以根据需要进行缩写 (例如 <code class="docutils literal notranslate"><span class="pre">-Wi</span></code>, <code class="docutils literal notranslate"><span class="pre">-Wd</span></code>, <code class="docutils literal notranslate"><span class="pre">-Wa</span></code>, <code class="docutils literal notranslate"><span class="pre">-We</span></code>)，解释器将会把它们解析为适当的操作名称。</p>
<p>请参阅 <a class="reference internal" href="../library/warnings.html#warning-filter"><span class="std std-ref">The Warnings Filter</span></a> 和 <a class="reference internal" href="../library/warnings.html#describing-warning-filters"><span class="std std-ref">Describing Warning Filters</span></a> 了解更多细节。</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-x">
<code class="descname">-x</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-x" title="永久链接至目标">¶</a></dt>
<dd><p>跳过源中第一行，以允许使用非 Unix 形式的 <code class="docutils literal notranslate"><span class="pre">#!cmd</span></code>。 这适用于 DOS 专属的破解操作。</p>
</dd></dl>

<dl class="cmdoption">
<dt id="id5">
<code class="descname">-X</code><code class="descclassname"></code><a class="headerlink" href="#id5" title="永久链接至目标">¶</a></dt>
<dd><p>保留用于各种具体实现专属的选项。 CPython 目前定义了下列可用的值：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">faulthandler</span></code> 启用 <a class="reference internal" href="../library/faulthandler.html#module-faulthandler" title="faulthandler: Dump the Python traceback."><code class="xref py py-mod docutils literal notranslate"><span class="pre">faulthandler</span></code></a>；</li>
<li><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">showrefcount</span></code> 当程序结束或在交互解释器中的每条语句之后输出总引用计数和已使用内存块计数。 此选项仅在调试版本中有效。</li>
<li><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">tracemalloc</span></code> 使用 <a class="reference internal" href="../library/tracemalloc.html#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> 模块启动对 Python 内存分配的跟踪。 默认情况下，只有最近的帧会保存在跟踪的回溯信息中。 使用 <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">tracemalloc=NFRAME</span></code> 以启动限定回溯 <em>NFRAME</em> 帧的跟踪。 请参阅 <a class="reference internal" href="../library/tracemalloc.html#tracemalloc.start" title="tracemalloc.start"><code class="xref py py-func docutils literal notranslate"><span class="pre">tracemalloc.start()</span></code></a> 了解详情。</li>
<li><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">showalloccount</span></code> 当程序结束时输出每种类型的已分配对象的总数。 此选项仅当 Python 在定义了 <code class="docutils literal notranslate"><span class="pre">COUNT_ALLOCS</span></code> 后构建时才会生效。</li>
<li><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">importtime</span></code> 显示每次导入耗费的时间。 它会显示模块名称，累计时间（包括嵌套的导入）和自身时间（排除嵌套的导入）。 请注意它的输出在多线程应用程序中可能会出错。 典型用法如 <code class="docutils literal notranslate"><span class="pre">python3</span> <span class="pre">-X</span> <span class="pre">importtime</span> <span class="pre">-c</span> <span class="pre">'import</span> <span class="pre">asyncio'</span></code>。 另请参阅 <span class="target" id="index-89"></span><a class="reference internal" href="#envvar-PYTHONPROFILEIMPORTTIME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPROFILEIMPORTTIME</span></code></a>。</li>
<li><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">dev</span></code>: 启用 CPython 的“开发模式”，引入额外的运行时检测，这些检测因开销过大而无法默认启用。 如果代码是正确的则它不会比默认输出更详细：新增警告只会在发现问题时才会发出。 开发模式的作用效果：<ul>
<li>添加 <code class="docutils literal notranslate"><span class="pre">default</span></code> 警告过滤器，即 <a class="reference internal" href="#cmdoption-w"><code class="xref std std-option docutils literal notranslate"><span class="pre">-W</span></code></a> <code class="docutils literal notranslate"><span class="pre">default</span></code>。</li>
<li>在内存分配器上安装调试钩子：参见 <a class="reference internal" href="../c-api/memory.html#c.PyMem_SetupDebugHooks" title="PyMem_SetupDebugHooks"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_SetupDebugHooks()</span></code></a> C 函数。</li>
<li>启用 <a class="reference internal" href="../library/faulthandler.html#module-faulthandler" title="faulthandler: Dump the Python traceback."><code class="xref py py-mod docutils literal notranslate"><span class="pre">faulthandler</span></code></a> 模块以在发生崩溃时转储 Python 回溯信息。</li>
<li>启用 <a class="reference internal" href="../library/asyncio-dev.html#asyncio-debug-mode"><span class="std std-ref">asyncio 调试模式</span></a>。</li>
<li>将 <a class="reference internal" href="../library/sys.html#sys.flags" title="sys.flags"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.flags</span></code></a> 的 <code class="xref py py-attr docutils literal notranslate"><span class="pre">dev_mode</span></code> 属性设为 <code class="docutils literal notranslate"><span class="pre">True</span></code></li>
</ul>
</li>
<li><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8</span></code> 为操作系统接口启用 UTF-8 模式，覆盖默认的区域感知模式。 <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8=0</span></code> 显式地禁用 UTF-8 模式（即使在它应当被自动激活的时候）。 请参阅 <span class="target" id="index-90"></span><a class="reference internal" href="#envvar-PYTHONUTF8"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONUTF8</span></code></a> 了解详情。</li>
</ul>
<p>它还允许传入任意值并通过 <a class="reference internal" href="../library/sys.html#sys._xoptions" title="sys._xoptions"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys._xoptions</span></code></a> 字典来提取这些值。</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.2 版更改: </span>增加了 <a class="reference internal" href="#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a> 选项。</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">3.3 新版功能: </span><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">faulthandler</span></code> 选项。</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">3.4 新版功能: </span><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">showrefcount</span></code> 与 <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">tracemalloc</span></code> 选项。</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">3.6 新版功能: </span><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">showalloccount</span></code> 选项。</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">3.7 新版功能: </span><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">importtime</span></code>, <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">dev</span></code> 与 <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8</span></code> 选项。</p>
</div>
</dd></dl>

</div>
<div class="section" id="options-you-shouldn-t-use">
<h3>1.1.4. 不应当使用的选项<a class="headerlink" href="#options-you-shouldn-t-use" title="永久链接至标题">¶</a></h3>
<dl class="cmdoption">
<dt id="cmdoption-j">
<code class="descname">-J</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-j" title="永久链接至目标">¶</a></dt>
<dd><p>保留给 <a class="reference external" href="http://www.jython.org/">Jython</a> 使用。</p>
</dd></dl>

</div>
</div>
<div class="section" id="environment-variables">
<span id="using-on-envvars"></span><h2>1.2. 环境变量<a class="headerlink" href="#environment-variables" title="永久链接至标题">¶</a></h2>
<p>这些环境变量会影响 Python 的行为，它们是在命令行开关之前被处理的，但 -E 或 -I 除外。 根据约定，当存在冲突时命令行开关会覆盖环境变量的设置。</p>
<dl class="envvar">
<dt id="envvar-PYTHONHOME">
<code class="descname">PYTHONHOME</code><a class="headerlink" href="#envvar-PYTHONHOME" title="永久链接至目标">¶</a></dt>
<dd><p>更改标准 Python 库的位置。 默认情况下库是在 <code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em><span class="pre">/lib/python</span><em><span class="pre">version</span></em></code> 和 <code class="file docutils literal notranslate"><em><span class="pre">exec_prefix</span></em><span class="pre">/lib/python</span><em><span class="pre">version</span></em></code> 中搜索，其中 <code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em></code> 和 <code class="file docutils literal notranslate"><em><span class="pre">exec_prefix</span></em></code> 是由安装位置确定的目录，默认都位于 <code class="file docutils literal notranslate"><span class="pre">/usr/local</span></code>。</p>
<p>当 <span class="target" id="index-91"></span><a class="reference internal" href="#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a> 被设为单个目录时，它的值会同时替代 <code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em></code> 和 <code class="file docutils literal notranslate"><em><span class="pre">exec_prefix</span></em></code>。 要为两者指定不同的值，请将 <span class="target" id="index-92"></span><a class="reference internal" href="#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a> 设为 <code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em><span class="pre">:</span><em><span class="pre">exec_prefix</span></em></code>。</p>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONPATH">
<code class="descname">PYTHONPATH</code><a class="headerlink" href="#envvar-PYTHONPATH" title="永久链接至目标">¶</a></dt>
<dd><p>增加模块文件默认搜索路径。 所用格式与终端的 <span class="target" id="index-93"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> 相同：一个或多个由 <a class="reference internal" href="../library/os.html#os.pathsep" title="os.pathsep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.pathsep</span></code></a> 分隔的目录路径名称（例如 Unix 上用冒号而在 Windows 上用分号）。 不存在的目录会被静默忽略。</p>
<p>In addition to normal directories, individual <span class="target" id="index-25"></span><a class="reference internal" href="#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> entries
may refer to zipfiles containing pure Python modules (in either source or
compiled form). Extension modules cannot be imported from zipfiles.</p>
<p>The default search path is installation dependent, but generally begins with
<code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em><span class="pre">/lib/python</span><em><span class="pre">version</span></em></code> (see <span class="target" id="index-26"></span><a class="reference internal" href="#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a> above).  It
is <em>always</em> appended to <span class="target" id="index-27"></span><a class="reference internal" href="#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a>.</p>
<p>An additional directory will be inserted in the search path in front of
<span class="target" id="index-28"></span><a class="reference internal" href="#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> as described above under
<a class="reference internal" href="#using-on-interface-options"><span class="std std-ref">接口选项</span></a>. The search path can be manipulated from
within a Python program as the variable <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>.</p>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONSTARTUP">
<code class="descname">PYTHONSTARTUP</code><a class="headerlink" href="#envvar-PYTHONSTARTUP" title="永久链接至目标">¶</a></dt>
<dd><p>If this is the name of a readable file, the Python commands in that file are
executed before the first prompt is displayed in interactive mode.  The file
is executed in the same namespace where interactive commands are executed so
that objects defined or imported in it can be used without qualification in
the interactive session.  You can also change the prompts <a class="reference internal" href="../library/sys.html#sys.ps1" title="sys.ps1"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.ps1</span></code></a> and
<a class="reference internal" href="../library/sys.html#sys.ps2" title="sys.ps2"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.ps2</span></code></a> and the hook <a class="reference internal" href="../library/sys.html#sys.__interactivehook__" title="sys.__interactivehook__"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.__interactivehook__</span></code></a> in this file.</p>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONOPTIMIZE">
<code class="descname">PYTHONOPTIMIZE</code><a class="headerlink" href="#envvar-PYTHONOPTIMIZE" title="永久链接至目标">¶</a></dt>
<dd><p>If this is set to a non-empty string it is equivalent to specifying the
<a class="reference internal" href="#cmdoption-o"><code class="xref std std-option docutils literal notranslate"><span class="pre">-O</span></code></a> option.  If set to an integer, it is equivalent to specifying
<a class="reference internal" href="#cmdoption-o"><code class="xref std std-option docutils literal notranslate"><span class="pre">-O</span></code></a> multiple times.</p>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONBREAKPOINT">
<code class="descname">PYTHONBREAKPOINT</code><a class="headerlink" href="#envvar-PYTHONBREAKPOINT" title="永久链接至目标">¶</a></dt>
<dd><p>If this is set, it names a callable using dotted-path notation.  The module
containing the callable will be imported and then the callable will be run
by the default implementation of <a class="reference internal" href="../library/sys.html#sys.breakpointhook" title="sys.breakpointhook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.breakpointhook()</span></code></a> which itself is
called by built-in <a class="reference internal" href="../library/functions.html#breakpoint" title="breakpoint"><code class="xref py py-func docutils literal notranslate"><span class="pre">breakpoint()</span></code></a>.  If not set, or set to the empty
string, it is equivalent to the value &quot;pdb.set_trace&quot;.  Setting this to the
string &quot;0&quot; causes the default implementation of <a class="reference internal" href="../library/sys.html#sys.breakpointhook" title="sys.breakpointhook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.breakpointhook()</span></code></a>
to do nothing but return immediately.</p>
<div class="versionadded">
<p><span class="versionmodified">3.7 新版功能.</span></p>
</div>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONDEBUG">
<code class="descname">PYTHONDEBUG</code><a class="headerlink" href="#envvar-PYTHONDEBUG" title="永久链接至目标">¶</a></dt>
<dd><p>If this is set to a non-empty string it is equivalent to specifying the
<a class="reference internal" href="#cmdoption-d"><code class="xref std std-option docutils literal notranslate"><span class="pre">-d</span></code></a> option.  If set to an integer, it is equivalent to specifying
<a class="reference internal" href="#cmdoption-d"><code class="xref std std-option docutils literal notranslate"><span class="pre">-d</span></code></a> multiple times.</p>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONINSPECT">
<code class="descname">PYTHONINSPECT</code><a class="headerlink" href="#envvar-PYTHONINSPECT" title="永久链接至目标">¶</a></dt>
<dd><p>If this is set to a non-empty string it is equivalent to specifying the
<a class="reference internal" href="#cmdoption-i"><code class="xref std std-option docutils literal notranslate"><span class="pre">-i</span></code></a> option.</p>
<p>This variable can also be modified by Python code using <a class="reference internal" href="../library/os.html#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a>
to force inspect mode on program termination.</p>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONUNBUFFERED">
<code class="descname">PYTHONUNBUFFERED</code><a class="headerlink" href="#envvar-PYTHONUNBUFFERED" title="永久链接至目标">¶</a></dt>
<dd><p>If this is set to a non-empty string it is equivalent to specifying the
<a class="reference internal" href="#cmdoption-u"><code class="xref std std-option docutils literal notranslate"><span class="pre">-u</span></code></a> option.</p>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONVERBOSE">
<code class="descname">PYTHONVERBOSE</code><a class="headerlink" href="#envvar-PYTHONVERBOSE" title="永久链接至目标">¶</a></dt>
<dd><p>If this is set to a non-empty string it is equivalent to specifying the
<a class="reference internal" href="#id4"><code class="xref std std-option docutils literal notranslate"><span class="pre">-v</span></code></a> option.  If set to an integer, it is equivalent to specifying
<a class="reference internal" href="#id4"><code class="xref std std-option docutils literal notranslate"><span class="pre">-v</span></code></a> multiple times.</p>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONCASEOK">
<code class="descname">PYTHONCASEOK</code><a class="headerlink" href="#envvar-PYTHONCASEOK" title="永久链接至目标">¶</a></dt>
<dd><p>If this is set, Python ignores case in <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statements.  This
only works on Windows and OS X.</p>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONDONTWRITEBYTECODE">
<code class="descname">PYTHONDONTWRITEBYTECODE</code><a class="headerlink" href="#envvar-PYTHONDONTWRITEBYTECODE" title="永久链接至目标">¶</a></dt>
<dd><p>If this is set to a non-empty string, Python won't try to write <code class="docutils literal notranslate"><span class="pre">.pyc</span></code>
files on the import of source modules.  This is equivalent to
specifying the <a class="reference internal" href="#id1"><code class="xref std std-option docutils literal notranslate"><span class="pre">-B</span></code></a> option.</p>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONHASHSEED">
<code class="descname">PYTHONHASHSEED</code><a class="headerlink" href="#envvar-PYTHONHASHSEED" title="永久链接至目标">¶</a></dt>
<dd><p>If this variable is not set or set to <code class="docutils literal notranslate"><span class="pre">random</span></code>, a random value is used
to seed the hashes of str, bytes and datetime objects.</p>
<p>If <span class="target" id="index-29"></span><a class="reference internal" href="#envvar-PYTHONHASHSEED"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHASHSEED</span></code></a> is set to an integer value, it is used as a fixed
seed for generating the hash() of the types covered by the hash
randomization.</p>
<p>Its purpose is to allow repeatable hashing, such as for selftests for the
interpreter itself, or to allow a cluster of python processes to share hash
values.</p>
<p>The integer must be a decimal number in the range [0,4294967295].  Specifying
the value 0 will disable hash randomization.</p>
<div class="versionadded">
<p><span class="versionmodified">3.2.3 新版功能.</span></p>
</div>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONIOENCODING">
<code class="descname">PYTHONIOENCODING</code><a class="headerlink" href="#envvar-PYTHONIOENCODING" title="永久链接至目标">¶</a></dt>
<dd><p>If this is set before running the interpreter, it overrides the encoding used
for stdin/stdout/stderr, in the syntax <code class="docutils literal notranslate"><span class="pre">encodingname:errorhandler</span></code>.  Both
the <code class="docutils literal notranslate"><span class="pre">encodingname</span></code> and the <code class="docutils literal notranslate"><span class="pre">:errorhandler</span></code> parts are optional and have
the same meaning as in <a class="reference internal" href="../library/stdtypes.html#str.encode" title="str.encode"><code class="xref py py-func docutils literal notranslate"><span class="pre">str.encode()</span></code></a>.</p>
<p>For stderr, the <code class="docutils literal notranslate"><span class="pre">:errorhandler</span></code> part is ignored; the handler will always be
<code class="docutils literal notranslate"><span class="pre">'backslashreplace'</span></code>.</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.4 版更改: </span>The <code class="docutils literal notranslate"><span class="pre">encodingname</span></code> part is now optional.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">在 3.6 版更改: </span>On Windows, the encoding specified by this variable is ignored for interactive
console buffers unless <span class="target" id="index-30"></span><a class="reference internal" href="#envvar-PYTHONLEGACYWINDOWSSTDIO"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONLEGACYWINDOWSSTDIO</span></code></a> is also specified.
Files and pipes redirected through the standard streams are not affected.</p>
</div>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONNOUSERSITE">
<code class="descname">PYTHONNOUSERSITE</code><a class="headerlink" href="#envvar-PYTHONNOUSERSITE" title="永久链接至目标">¶</a></dt>
<dd><p>If this is set, Python won't add the <a class="reference internal" href="../library/site.html#site.USER_SITE" title="site.USER_SITE"><code class="xref py py-data docutils literal notranslate"><span class="pre">user</span> <span class="pre">site-packages</span> <span class="pre">directory</span></code></a> to <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>.</p>
<div class="admonition seealso">
<p class="first admonition-title">参见</p>
<p class="last"><span class="target" id="index-94"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0370"><strong>PEP 370</strong></a> -- 分用户的 site-packages 目录</p>
</div>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONUSERBASE">
<code class="descname">PYTHONUSERBASE</code><a class="headerlink" href="#envvar-PYTHONUSERBASE" title="永久链接至目标">¶</a></dt>
<dd><p>Defines the <a class="reference internal" href="../library/site.html#site.USER_BASE" title="site.USER_BASE"><code class="xref py py-data docutils literal notranslate"><span class="pre">user</span> <span class="pre">base</span> <span class="pre">directory</span></code></a>, which is used to
compute the path of the <a class="reference internal" href="../library/site.html#site.USER_SITE" title="site.USER_SITE"><code class="xref py py-data docutils literal notranslate"><span class="pre">user</span> <span class="pre">site-packages</span> <span class="pre">directory</span></code></a>
and <a class="reference internal" href="../install/index.html#inst-alt-install-user"><span class="std std-ref">Distutils installation paths</span></a> for
<code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">install</span> <span class="pre">--user</span></code>.</p>
<div class="admonition seealso">
<p class="first admonition-title">参见</p>
<p class="last"><span class="target" id="index-95"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0370"><strong>PEP 370</strong></a> -- 分用户的 site-packages 目录</p>
</div>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONEXECUTABLE">
<code class="descname">PYTHONEXECUTABLE</code><a class="headerlink" href="#envvar-PYTHONEXECUTABLE" title="永久链接至目标">¶</a></dt>
<dd><p>If this environment variable is set, <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> will be set to its
value instead of the value got through the C runtime.  Only works on
Mac OS X.</p>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONWARNINGS">
<code class="descname">PYTHONWARNINGS</code><a class="headerlink" href="#envvar-PYTHONWARNINGS" title="永久链接至目标">¶</a></dt>
<dd><p>This is equivalent to the <a class="reference internal" href="#cmdoption-w"><code class="xref std std-option docutils literal notranslate"><span class="pre">-W</span></code></a> option. If set to a comma
separated string, it is equivalent to specifying <a class="reference internal" href="#cmdoption-w"><code class="xref std std-option docutils literal notranslate"><span class="pre">-W</span></code></a> multiple
times, with filters later in the list taking precedence over those earlier
in the list.</p>
<p>最简单的设置是将某个特定操作无条件地应用于进程所发出所有警告 (即使是在默认情况下会忽略的那些警告):</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><span class="nv">PYTHONWARNINGS</span><span class="o">=</span>default  <span class="c1"># Warn once per call location</span>
<span class="nv">PYTHONWARNINGS</span><span class="o">=</span>error    <span class="c1"># Convert to exceptions</span>
<span class="nv">PYTHONWARNINGS</span><span class="o">=</span>always   <span class="c1"># Warn every time</span>
<span class="nv">PYTHONWARNINGS</span><span class="o">=</span>module   <span class="c1"># Warn once per calling module</span>
<span class="nv">PYTHONWARNINGS</span><span class="o">=</span>once     <span class="c1"># Warn once per Python process</span>
<span class="nv">PYTHONWARNINGS</span><span class="o">=</span>ignore   <span class="c1"># Never warn</span>
</pre></div>
</div>
<p>请参阅 <a class="reference internal" href="../library/warnings.html#warning-filter"><span class="std std-ref">The Warnings Filter</span></a> 和 <a class="reference internal" href="../library/warnings.html#describing-warning-filters"><span class="std std-ref">Describing Warning Filters</span></a> 了解更多细节。</p>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONFAULTHANDLER">
<code class="descname">PYTHONFAULTHANDLER</code><a class="headerlink" href="#envvar-PYTHONFAULTHANDLER" title="永久链接至目标">¶</a></dt>
<dd><p>If this environment variable is set to a non-empty string,
<a class="reference internal" href="../library/faulthandler.html#faulthandler.enable" title="faulthandler.enable"><code class="xref py py-func docutils literal notranslate"><span class="pre">faulthandler.enable()</span></code></a> is called at startup: install a handler for
<code class="xref py py-const docutils literal notranslate"><span class="pre">SIGSEGV</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGFPE</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGABRT</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGBUS</span></code> and
<code class="xref py py-const docutils literal notranslate"><span class="pre">SIGILL</span></code> signals to dump the Python traceback.  This is equivalent to
<a class="reference internal" href="#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a> <code class="docutils literal notranslate"><span class="pre">faulthandler</span></code> option.</p>
<div class="versionadded">
<p><span class="versionmodified">3.3 新版功能.</span></p>
</div>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONTRACEMALLOC">
<code class="descname">PYTHONTRACEMALLOC</code><a class="headerlink" href="#envvar-PYTHONTRACEMALLOC" title="永久链接至目标">¶</a></dt>
<dd><p>If this environment variable is set to a non-empty string, start tracing
Python memory allocations using the <a class="reference internal" href="../library/tracemalloc.html#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module. The value of
the variable is the maximum number of frames stored in a traceback of a
trace. For example, <code class="docutils literal notranslate"><span class="pre">PYTHONTRACEMALLOC=1</span></code> stores only the most recent
frame. See the <a class="reference internal" href="../library/tracemalloc.html#tracemalloc.start" title="tracemalloc.start"><code class="xref py py-func docutils literal notranslate"><span class="pre">tracemalloc.start()</span></code></a> for more information.</p>
<div class="versionadded">
<p><span class="versionmodified">3.4 新版功能.</span></p>
</div>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONPROFILEIMPORTTIME">
<code class="descname">PYTHONPROFILEIMPORTTIME</code><a class="headerlink" href="#envvar-PYTHONPROFILEIMPORTTIME" title="永久链接至目标">¶</a></dt>
<dd><p>If this environment variable is set to a non-empty string, Python will
show how long each import takes.  This is exactly equivalent to setting
<code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">importtime</span></code> on the command line.</p>
<div class="versionadded">
<p><span class="versionmodified">3.7 新版功能.</span></p>
</div>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONASYNCIODEBUG">
<code class="descname">PYTHONASYNCIODEBUG</code><a class="headerlink" href="#envvar-PYTHONASYNCIODEBUG" title="永久链接至目标">¶</a></dt>
<dd><p>If this environment variable is set to a non-empty string, enable the
<a class="reference internal" href="../library/asyncio-dev.html#asyncio-debug-mode"><span class="std std-ref">debug mode</span></a> of the <a class="reference internal" href="../library/asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code></a> module.</p>
<div class="versionadded">
<p><span class="versionmodified">3.4 新版功能.</span></p>
</div>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONMALLOC">
<code class="descname">PYTHONMALLOC</code><a class="headerlink" href="#envvar-PYTHONMALLOC" title="永久链接至目标">¶</a></dt>
<dd><p>Set the Python memory allocators and/or install debug hooks.</p>
<p>Set the family of memory allocators used by Python:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">default</span></code>: use the <a class="reference internal" href="../c-api/memory.html#default-memory-allocators"><span class="std std-ref">default memory allocators</span></a>.</li>
<li><code class="docutils literal notranslate"><span class="pre">malloc</span></code>: use the <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code> function of the C library
for all domains (<a class="reference internal" href="../c-api/memory.html#c.PYMEM_DOMAIN_RAW" title="PYMEM_DOMAIN_RAW"><code class="xref c c-data docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_RAW</span></code></a>, <a class="reference internal" href="../c-api/memory.html#c.PYMEM_DOMAIN_MEM" title="PYMEM_DOMAIN_MEM"><code class="xref c c-data docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_MEM</span></code></a>,
<a class="reference internal" href="../c-api/memory.html#c.PYMEM_DOMAIN_OBJ" title="PYMEM_DOMAIN_OBJ"><code class="xref c c-data docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_OBJ</span></code></a>).</li>
<li><code class="docutils literal notranslate"><span class="pre">pymalloc</span></code>: use the <a class="reference internal" href="../c-api/memory.html#pymalloc"><span class="std std-ref">pymalloc allocator</span></a> for
<a class="reference internal" href="../c-api/memory.html#c.PYMEM_DOMAIN_MEM" title="PYMEM_DOMAIN_MEM"><code class="xref c c-data docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_MEM</span></code></a> and <a class="reference internal" href="../c-api/memory.html#c.PYMEM_DOMAIN_OBJ" title="PYMEM_DOMAIN_OBJ"><code class="xref c c-data docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_OBJ</span></code></a> domains and use
the <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code> function for the <a class="reference internal" href="../c-api/memory.html#c.PYMEM_DOMAIN_RAW" title="PYMEM_DOMAIN_RAW"><code class="xref c c-data docutils literal notranslate"><span class="pre">PYMEM_DOMAIN_RAW</span></code></a> domain.</li>
</ul>
<p>Install debug hooks:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">debug</span></code>: install debug hooks on top of the <a class="reference internal" href="../c-api/memory.html#default-memory-allocators"><span class="std std-ref">default memory
allocators</span></a>.</li>
<li><code class="docutils literal notranslate"><span class="pre">malloc_debug</span></code>: same as <code class="docutils literal notranslate"><span class="pre">malloc</span></code> but also install debug hooks</li>
<li><code class="docutils literal notranslate"><span class="pre">pymalloc_debug</span></code>: same as <code class="docutils literal notranslate"><span class="pre">pymalloc</span></code> but also install debug hooks</li>
</ul>
<p>See the <a class="reference internal" href="../c-api/memory.html#default-memory-allocators"><span class="std std-ref">default memory allocators</span></a> and the
<a class="reference internal" href="../c-api/memory.html#c.PyMem_SetupDebugHooks" title="PyMem_SetupDebugHooks"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_SetupDebugHooks()</span></code></a> function (install debug hooks on Python
memory allocators).</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.7 版更改: </span>Added the <code class="docutils literal notranslate"><span class="pre">&quot;default&quot;</span></code> allocator.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">3.6 新版功能.</span></p>
</div>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONMALLOCSTATS">
<code class="descname">PYTHONMALLOCSTATS</code><a class="headerlink" href="#envvar-PYTHONMALLOCSTATS" title="永久链接至目标">¶</a></dt>
<dd><p>If set to a non-empty string, Python will print statistics of the
<a class="reference internal" href="../c-api/memory.html#pymalloc"><span class="std std-ref">pymalloc memory allocator</span></a> every time a new pymalloc object
arena is created, and on shutdown.</p>
<p>This variable is ignored if the <span class="target" id="index-33"></span><a class="reference internal" href="#envvar-PYTHONMALLOC"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONMALLOC</span></code></a> environment variable
is used to force the <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code> allocator of the C library, or if
Python is configured without <code class="docutils literal notranslate"><span class="pre">pymalloc</span></code> support.</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.6 版更改: </span>This variable can now also be used on Python compiled in release mode.
It now has no effect if set to an empty string.</p>
</div>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONLEGACYWINDOWSFSENCODING">
<code class="descname">PYTHONLEGACYWINDOWSFSENCODING</code><a class="headerlink" href="#envvar-PYTHONLEGACYWINDOWSFSENCODING" title="永久链接至目标">¶</a></dt>
<dd><p>If set to a non-empty string, the default filesystem encoding and errors mode
will revert to their pre-3.6 values of 'mbcs' and 'replace', respectively.
Otherwise, the new defaults 'utf-8' and 'surrogatepass' are used.</p>
<p>This may also be enabled at runtime with
<a class="reference internal" href="../library/sys.html#sys._enablelegacywindowsfsencoding" title="sys._enablelegacywindowsfsencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys._enablelegacywindowsfsencoding()</span></code></a>.</p>
<p class="availability"><a class="reference internal" href="../library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p>
<div class="versionadded">
<p><span class="versionmodified">3.6 新版功能: </span>有关更多详细信息，请参阅 <span class="target" id="index-96"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0529"><strong>PEP 529</strong></a>。</p>
</div>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONLEGACYWINDOWSSTDIO">
<code class="descname">PYTHONLEGACYWINDOWSSTDIO</code><a class="headerlink" href="#envvar-PYTHONLEGACYWINDOWSSTDIO" title="永久链接至目标">¶</a></dt>
<dd><p>If set to a non-empty string, does not use the new console reader and
writer. This means that Unicode characters will be encoded according to
the active console code page, rather than using utf-8.</p>
<p>This variable is ignored if the standard streams are redirected (to files
or pipes) rather than referring to console buffers.</p>
<p class="availability"><a class="reference internal" href="../library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p>
<div class="versionadded">
<p><span class="versionmodified">3.6 新版功能.</span></p>
</div>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONCOERCECLOCALE">
<code class="descname">PYTHONCOERCECLOCALE</code><a class="headerlink" href="#envvar-PYTHONCOERCECLOCALE" title="永久链接至目标">¶</a></dt>
<dd><p>If set to the value <code class="docutils literal notranslate"><span class="pre">0</span></code>, causes the main Python command line application
to skip coercing the legacy ASCII-based C and POSIX locales to a more
capable UTF-8 based alternative.</p>
<p>If this variable is <em>not</em> set (or is set to a value other than <code class="docutils literal notranslate"><span class="pre">0</span></code>), the
<code class="docutils literal notranslate"><span class="pre">LC_ALL</span></code> locale override environment variable is also not set, and the
current locale reported for the <code class="docutils literal notranslate"><span class="pre">LC_CTYPE</span></code> category is either the default
<code class="docutils literal notranslate"><span class="pre">C</span></code> locale, or else the explicitly ASCII-based <code class="docutils literal notranslate"><span class="pre">POSIX</span></code> locale, then the
Python CLI will attempt to configure the following locales for the
<code class="docutils literal notranslate"><span class="pre">LC_CTYPE</span></code> category in the order listed before loading the interpreter
runtime:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">C.UTF-8</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">C.utf8</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">UTF-8</span></code></li>
</ul>
<p>If setting one of these locale categories succeeds, then the <code class="docutils literal notranslate"><span class="pre">LC_CTYPE</span></code>
environment variable will also be set accordingly in the current process
environment before the Python runtime is initialized. This ensures that in
addition to being seen by both the interpreter itself and other locale-aware
components running in the same process (such as the GNU <code class="docutils literal notranslate"><span class="pre">readline</span></code>
library), the updated setting is also seen in subprocesses (regardless of
whether or not those processes are running a Python interpreter), as well as
in operations that query the environment rather than the current C locale
(such as Python's own <a class="reference internal" href="../library/locale.html#locale.getdefaultlocale" title="locale.getdefaultlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.getdefaultlocale()</span></code></a>).</p>
<p>Configuring one of these locales (either explicitly or via the above
implicit locale coercion) automatically enables the <code class="docutils literal notranslate"><span class="pre">surrogateescape</span></code>
<a class="reference internal" href="../library/codecs.html#error-handlers"><span class="std std-ref">error handler</span></a> for <a class="reference internal" href="../library/sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a> and
<a class="reference internal" href="../library/sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a> (<a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> continues to use <code class="docutils literal notranslate"><span class="pre">backslashreplace</span></code>
as it does in any other locale). This stream handling behavior can be
overridden using <span class="target" id="index-35"></span><a class="reference internal" href="#envvar-PYTHONIOENCODING"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONIOENCODING</span></code></a> as usual.</p>
<p>For debugging purposes, setting <code class="docutils literal notranslate"><span class="pre">PYTHONCOERCECLOCALE=warn</span></code> will cause
Python to emit warning messages on <code class="docutils literal notranslate"><span class="pre">stderr</span></code> if either the locale coercion
activates, or else if a locale that <em>would</em> have triggered coercion is
still active when the Python runtime is initialized.</p>
<p>Also note that even when locale coercion is disabled, or when it fails to
find a suitable target locale, <span class="target" id="index-36"></span><a class="reference internal" href="#envvar-PYTHONUTF8"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONUTF8</span></code></a> will still activate by
default in legacy ASCII-based locales. Both features must be disabled in
order to force the interpreter to use <code class="docutils literal notranslate"><span class="pre">ASCII</span></code> instead of <code class="docutils literal notranslate"><span class="pre">UTF-8</span></code> for
system interfaces.</p>
<p class="availability"><a class="reference internal" href="../library/intro.html#availability"><span class="std std-ref">Availability</span></a>: *nix.</p>
<div class="versionadded">
<p><span class="versionmodified">3.7 新版功能: </span>See <span class="target" id="index-37"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0538"><strong>PEP 538</strong></a> for more details.</p>
</div>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONDEVMODE">
<code class="descname">PYTHONDEVMODE</code><a class="headerlink" href="#envvar-PYTHONDEVMODE" title="永久链接至目标">¶</a></dt>
<dd><p>If this environment variable is set to a non-empty string, enable the
CPython &quot;development mode&quot;. See the <a class="reference internal" href="#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a> <code class="docutils literal notranslate"><span class="pre">dev</span></code> option.</p>
<div class="versionadded">
<p><span class="versionmodified">3.7 新版功能.</span></p>
</div>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONUTF8">
<code class="descname">PYTHONUTF8</code><a class="headerlink" href="#envvar-PYTHONUTF8" title="永久链接至目标">¶</a></dt>
<dd><p>If set to <code class="docutils literal notranslate"><span class="pre">1</span></code>, enables the interpreter's UTF-8 mode, where <code class="docutils literal notranslate"><span class="pre">UTF-8</span></code> is
used as the text encoding for system interfaces, regardless of the
current locale setting.</p>
<p>This means that:</p>
<blockquote>
<div><ul class="simple">
<li><a class="reference internal" href="../library/sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getfilesystemencoding()</span></code></a> returns <code class="docutils literal notranslate"><span class="pre">'UTF-8'</span></code> (the locale
encoding is ignored).</li>
<li><a class="reference internal" href="../library/locale.html#locale.getpreferredencoding" title="locale.getpreferredencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.getpreferredencoding()</span></code></a> returns <code class="docutils literal notranslate"><span class="pre">'UTF-8'</span></code> (the locale
encoding is ignored, and the function's <code class="docutils literal notranslate"><span class="pre">do_setlocale</span></code> parameter has no
effect).</li>
<li><a class="reference internal" href="../library/sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a>, <a class="reference internal" href="../library/sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a>, and <a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> all use
UTF-8 as their text encoding, with the <code class="docutils literal notranslate"><span class="pre">surrogateescape</span></code>
<a class="reference internal" href="../library/codecs.html#error-handlers"><span class="std std-ref">error handler</span></a> being enabled for <a class="reference internal" href="../library/sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a>
and <a class="reference internal" href="../library/sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a> (<a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> continues to use
<code class="docutils literal notranslate"><span class="pre">backslashreplace</span></code> as it does in the default locale-aware mode)</li>
</ul>
</div></blockquote>
<p>As a consequence of the changes in those lower level APIs, other higher
level APIs also exhibit different default behaviours:</p>
<blockquote>
<div><ul class="simple">
<li>Command line arguments, environment variables and filenames are decoded
to text using the UTF-8 encoding.</li>
<li><a class="reference internal" href="../library/os.html#os.fsdecode" title="os.fsdecode"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fsdecode()</span></code></a> and <a class="reference internal" href="../library/os.html#os.fsencode" title="os.fsencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fsencode()</span></code></a> use the UTF-8 encoding.</li>
<li><a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>, <a class="reference internal" href="../library/io.html#io.open" title="io.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">io.open()</span></code></a>, and <a class="reference internal" href="../library/codecs.html#codecs.open" title="codecs.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">codecs.open()</span></code></a> use the UTF-8
encoding by default. However, they still use the strict error handler by
default so that attempting to open a binary file in text mode is likely
to raise an exception rather than producing nonsense data.</li>
</ul>
</div></blockquote>
<p>Note that the standard stream settings in UTF-8 mode can be overridden by
<span class="target" id="index-38"></span><a class="reference internal" href="#envvar-PYTHONIOENCODING"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONIOENCODING</span></code></a> (just as they can be in the default locale-aware
mode).</p>
<p>If set to <code class="docutils literal notranslate"><span class="pre">0</span></code>, the interpreter runs in its default locale-aware mode.</p>
<p>Setting any other non-empty string causes an error during interpreter
initialisation.</p>
<p>If this environment variable is not set at all, then the interpreter defaults
to using the current locale settings, <em>unless</em> the current locale is
identified as a legacy ASCII-based locale
(as described for <span class="target" id="index-39"></span><a class="reference internal" href="#envvar-PYTHONCOERCECLOCALE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONCOERCECLOCALE</span></code></a>), and locale coercion is
either disabled or fails. In such legacy locales, the interpreter will
default to enabling UTF-8 mode unless explicitly instructed not to do so.</p>
<p>Also available as the <a class="reference internal" href="#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a> <code class="docutils literal notranslate"><span class="pre">utf8</span></code> option.</p>
<p class="availability"><a class="reference internal" href="../library/intro.html#availability"><span class="std std-ref">Availability</span></a>: *nix.</p>
<div class="versionadded">
<p><span class="versionmodified">3.7 新版功能: </span>See <span class="target" id="index-40"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0540"><strong>PEP 540</strong></a> for more details.</p>
</div>
</dd></dl>

<div class="section" id="debug-mode-variables">
<h3>1.2.1. Debug-mode variables<a class="headerlink" href="#debug-mode-variables" title="永久链接至标题">¶</a></h3>
<p>Setting these variables only has an effect in a debug build of Python, that is,
if Python was configured with the <code class="docutils literal notranslate"><span class="pre">--with-pydebug</span></code> build option.</p>
<dl class="envvar">
<dt id="envvar-PYTHONTHREADDEBUG">
<code class="descname">PYTHONTHREADDEBUG</code><a class="headerlink" href="#envvar-PYTHONTHREADDEBUG" title="永久链接至目标">¶</a></dt>
<dd><p>If set, Python will print threading debug info.</p>
</dd></dl>

<dl class="envvar">
<dt id="envvar-PYTHONDUMPREFS">
<code class="descname">PYTHONDUMPREFS</code><a class="headerlink" href="#envvar-PYTHONDUMPREFS" title="永久链接至目标">¶</a></dt>
<dd><p>If set, Python will dump objects and reference counts still alive after
shutting down the interpreter.</p>
</dd></dl>

</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">1. 命令行与环境</a><ul>
<li><a class="reference internal" href="#command-line">1.1. 命令行</a><ul>
<li><a class="reference internal" href="#interface-options">1.1.1. 接口选项</a></li>
<li><a class="reference internal" href="#generic-options">1.1.2. 通用选项</a></li>
<li><a class="reference internal" href="#miscellaneous-options">1.1.3. 其他选项</a></li>
<li><a class="reference internal" href="#options-you-shouldn-t-use">1.1.4. 不应当使用的选项</a></li>
</ul>
</li>
<li><a class="reference internal" href="#environment-variables">1.2. 环境变量</a><ul>
<li><a class="reference internal" href="#debug-mode-variables">1.2.1. Debug-mode variables</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="index.html"
                        title="上一章">安装和使用 Python</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="unix.html"
                        title="下一章">2. 在Unix平台中使用Python</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/using/cmdline.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="unix.html" title="2. 在Unix平台中使用Python"
             >下一页</a> |</li>
        <li class="right" >
          <a href="index.html" title="安装和使用 Python"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh_CN</span>
          <span class="version_switcher_placeholder">3.7.3</span>
          <a href="../index.html">文档</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >安装和使用 Python</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2019, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 4月 09, 2019.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>1.8.4 创建。
    </div>

  </body>
</html>